import React from 'react';
import BaseFilter, { filterCodes } from '../Base/BaseFilter';
// import DatePicker from "../DatePicker/DatePicker";
import { forEach, isArray, isEmpty, isNumber, map } from 'lodash-es';
import {
    dateStr2Timestamp, isDateIns, myFormatTime, newDateIns,
    gmt0ToCurrentTimestamp
} from 'jh-utils';
import { getDerivedStateFromPropsFunc } from '../Base/helper/lifeCycle';
import BaseField from '../Base/BaseField';
import BaseFormItem from '../Base/BaseFormItem';
import DateComp from './index';

// const {RangePicker} = DatePicker;

class DateFilterBase extends BaseFilter {

    static getDerivedStateFromProps(nextProps, preState) {
        let reObj = BaseField.getDerivedStateFromProps(nextProps, preState);
        return getDerivedStateFromPropsFunc(nextProps, preState, (changedValue) => {
            // @param changedValue 即 nextProps 中有变化的 value 或 defaultValue 的值。
            // 在这个方法里返回需要更新到 state 里的值。
            // （注：defaultValue 的处理已经在 getDerivedStateFromPropsFunc 中默认处理，所以这里不用返回了）
            let { comboVal, compVal } = changedValue || {};
            // 将 compVal 转成 dayjs
            //  如果 compVal 是时间戳，则把 compVal 当成 GMT0 时区的时间戳，所以需要先将 compVal 转成当前用户时区的时间戳，再 newDateIns
            // if (isNumber(compVal)) {
            // 	console.log('DateFilterBase gdsfp:', compVal);
            // 	compVal = gmt0ToCurrentTimestamp(compVal);
            // 	console.log('DateFilterBase gdsfp after:', compVal);
            // 	console.log('DateFilterBase gdsfp newDateIns(compVal):', newDateIns(compVal));
            // }
            // const dayIns = newDateIns(compVal);
            // console.log('DateFilterBase gdsfp dayIns:', dayIns);
            return {
                ...(reObj || {}),
                comboVal: comboVal || nextProps.defaultFilterCode,
                compVal,
                // compVal: 0 < compVal && dayIns.isValid() ? dayIns : undefined,
            };
        });
    }

    comboData = [
        filterCodes.between,
        // filterCodes.today,
        // filterCodes.day3, filterCodes.week1, filterCodes.month1,
        filterCodes.eq, filterCodes.noEq,
        filterCodes.gt, filterCodes.gtEq,
        filterCodes.lt, filterCodes.ltEq,
        filterCodes.noNull, filterCodes.null,
    ];

    initState(props) {
        const state = super.initState(props);
        let { compVal } = props.value || props.defaultValue || {};
        // 将 compVal 转成 dayjs
        //  如果 compVal 是时间戳，则把 compVal 当成 GMT0 时区的时间戳，所以需要先将 compVal 转成当前用户时区的时间戳，再 newDateIns
        // if (isNumber(compVal)) {
        // 	compVal = gmt0ToCurrentTimestamp(compVal);
        // }
        // const dayIns = newDateIns(compVal);
        return {
            ...state,
            compVal,
            // compVal: 0 < compVal && dayIns.isValid() ? dayIns : undefined,
        };
    }

    getAjaxData(newValues = {}) {
        // 需要将日期转成时间戳
        let reObj = super.getAjaxData(newValues);
        let { comboVal, compVal } = reObj;

        if (compVal && filterCodes.between.code === comboVal) {
            reObj.compVal = map(compVal, (valItem) => {

                if (isDateIns(valItem)) {
                    return dateStr2Timestamp(valItem);
                } else {
                    return valItem;
                }
            });

        } else if (isDateIns(compVal)) {
            reObj.compVal = dateStr2Timestamp(compVal);
        }

        console.log('DateFilterBase getAjaxData reObj.compVal', reObj.compVal);

        reObj.filterVal = this.dealValue4Filter(reObj);

        return reObj;
    }

    /**
     * 枚举选择框的数据有变化时的回调
     * @param value
     */
    hdlComboChange(value) {
        let compVal = this.state.compVal;

        if (filterCodes.between.code === value) {

            if (!isArray(compVal) && !isEmpty(compVal)) {
                compVal = [compVal];
            } else {
                compVal = undefined;
            }
        } else {

            if (isArray(compVal)) {
                compVal = compVal[0];
            }
        }

        // 作为受控组件
        if ('value' in this.props) {
            this.props.onChange && this.props.onChange(this.getAjaxData({ comboVal: value, compVal }));

        } else {
            this.setState({ comboVal: value, compVal }, () => {
                this.props.onChange && this.props.onChange(this.getAjaxData());
            });
        }
    }

    compRender(moreProps = {}) {

        if (filterCodes.between.code === this.state.comboVal) { // 介于

            return <DateComp {...moreProps} range={true}/>; //<RangePicker {...moreProps} allowClear showTime/>;

        } else if (
            -1 !== [
                filterCodes.week1.code, filterCodes.today.code,
                filterCodes.day3.code, filterCodes.month1.code,
            ].indexOf(this.state.comboVal)
        ) {
            return null;
        } else {
            return (<DateComp {...moreProps}/>);
        }

    }

}

export default class DateFilter extends BaseFormItem {
    Comp = DateFilterBase;
}